#ifndef _CXYCHART_H_
#define _CXYCHART_H_

#include <buola/chart/cchart.h>

#include <buola/chart/clegend.h>
#include <buola/utility/usignal.h>

namespace buola { namespace chart {

struct CXYChartContext
{
    PXYChart mChart;

    img::CGC *mGC;
    CRect_d mRect;

    CXAxis *mXAxis;
    CYAxis *mYAxis;

    double mMinX;
    double mMaxX;
    double mMinY;
    double mMaxY;
    double mMinPosX;
    double mMinPosY;
    CRect_d mExtraSpace;

    CLegend *mLegend;

    PXYPlot mTraceLock;
};

//!base class for XY charts
class CXYChart : public CChart
{
    struct SMarker
    {
        img::CColor mColor;
        std::wstring mText;
        CPoint_d mDataPos;

        double mRadius;
        CPoint_d mExactPos;
        CPoint_d mRealPos;
    };

public:
    CXYChart();
    ~CXYChart();

    //!add a new plot
    void AddPlot(PXYPlot pPlot);
    //!remove a previously added plot
    void RemovePlot(PXYPlot pPlot);
    //!remove all plots
    void RemovePlots();

    //!returns a pointer to the X axis
    CXAxis *GetXAxis()             {   return mXAxis;  }
    //!returns a pointer to the Y axis
    CYAxis *GetYAxis()             {   return mYAxis;  }

    //!returns a reference to the list of plots
    const std::vector<PXYPlot> &Plots()  {   return mPlots;  }

//    virtual void Zoom(const CRect_d &pZoomArea);
//    virtual void ZoomAll();
//    virtual void FixZoomRect(CRect_d&);

    void AddMarker(const img::CColor &pColor,const std::wstring &pText,const CPoint_d &pPos);
    void RemoveAllMarkers();

private:
    virtual void OnButtonDown(const gui::CMouseEvent &pE);
    virtual void OnExpose(const gui::CExposeEvent &pE);

    PXYPlot GetPlotAt(const CPoint_d &pPos);

    void PlaceMarkers(img::CGC *pGC);

public:
    //!signal
    USignal<void(PXYPlot)> sPlotLocked;
    USignal<void(const std::wstring&)> sMarkerClicked;

private:
    std::vector<PXYPlot> mPlots;

    PXYPlot mTraceLock;

    CXAxis *mXAxis;
    CYAxis *mYAxis;
    CLegend *mLegend;

    //! \name data for interaction
    CRect_d mDataRect;

    std::vector<std::unique_ptr<SMarker>> mMarkers;
};

/*namespace chart*/ } /*namespace buola*/ }

#endif
